/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.representation;

import org.openaecfm.model.representation.CoordinateOperation;
import org.openaecfm.model.representation.MapConversion;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class MapConversionIO extends
		AbstractEntityInstanceInputImpl<MapConversion> implements
		IEntityInstanceOutput<MapConversion> {

	private IEntityInstanceUtilOutput<CoordinateOperation> utilOutput;
	private IEntityInstanceUtilInput<CoordinateOperation> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<CoordinateOperation> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<CoordinateOperation> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(MapConversion pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCTOPOLOGYREPRESENTATION", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;
		
		// Set Eastings
		sei.addParameter(new RealParameter(pojo.getEastings()));

		// Set Northings
		sei.addParameter(new RealParameter(pojo.getNorthings()));

		// Set OrthogonalHeight
		sei.addParameter(new RealParameter(pojo.getOrthogonalHeight()));

		// Set XAxisAbscissa
		if (pojo.getXAxisAbscissa() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getXAxisAbscissa()));
		}

		// Set XAxisOrdinate
		if (pojo.getXAxisOrdinate() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getXAxisOrdinate()));
		}

		// Set Scale
		if (pojo.getScale() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getScale()));
		}

		return ei.toString();
	}

	@Override
	protected MapConversion doCreateObject() {
		return new MapConversion();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			MapConversion pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Eastings
		RealParameter eastings = (RealParameter) sei.getParameter(2);
		pojo.setEastings(eastings.toDouble());

		// Set Northings
		RealParameter northings = (RealParameter) sei.getParameter(3);
		pojo.setNorthings(northings.toDouble());

		// Set OrthogonalHeight
		RealParameter orthogonalHeight = (RealParameter) sei.getParameter(4);
		pojo.setOrthogonalHeight(orthogonalHeight.toDouble());

		// Set XAxisAbscissa
		Parameter xAxisAbscissa = sei.getParameter(5);
		if (xAxisAbscissa.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) xAxisAbscissa;
			pojo.setXAxisAbscissa(r.toDouble());
		}

		// Set XAxisOrdinate
		Parameter xAxisOrdinate = sei.getParameter(6);
		if (xAxisOrdinate.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) xAxisOrdinate;
			pojo.setXAxisOrdinate(r.toDouble());
		}

		// Set Scale
		Parameter scale = sei.getParameter(7);
		if (scale.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) scale;
			pojo.setScale(r.toDouble());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			MapConversion pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}
}
